layui.use(['form','layer'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    $('#pName').click(function(){
        layui.layer.open({
            type: 1,
            content: $('#ztreeShow'),
            btn: ['确定', '取消'],
            area: ['400px', '600px'],
            yes: function(index, layero){
                layui.layer.close(layui.layer.index)
            },
            btn2: function(index, layero){
                //按钮【按钮二】的回调
                $('#pName').val('');
                $('#pId').val(0);
                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
    })

    function zTreeOnClick(event, treeId, treeNode) {
        $('#pName').val(treeNode.deptName);
        $('#pId').val(treeNode.id);
    }

    function zTreeOnDblClick(event, treeId, treeNode) {
        layui.layer.close(layui.layer.index);
    };

    var setting = {
        view: {
            selectedMulti: false
        },
        callback: {
            onClick: zTreeOnClick,
            onDblClick: zTreeOnDblClick
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: 0
            },
            key: {
                name: "deptName"
            }
        }
    }

    $(document).ready(function(){
        //zTreeObj = $.fn.zTree.init($("#tree"), setting, zTreeNodes);
        $.ajax({
            type:"get",
            url:"http://localhost/admin/sysDept/getTreeList",
            dataType:"json",
            success:function(data){
                $.fn.zTree.init($("#tree"), setting, data.data);
                $.fn.zTree.getZTreeObj("tree").expandAll(true);
            }
        })
    });

    form.on("submit(updateDept)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type: 'post',
            url: 'http://localhost/admin/sysDept/updateDept',
            data: form.val("formFilter"),
            dataType:"json",
            success:function(data){
                if(data.code == 0){
                    setTimeout(function(){
                        top.layer.close(index);
                        top.layer.msg(data.msg);
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    },2000);
                }else{
                    layer.msg(data.msg, {
                        icon: 5,
                        time: 1500 //2秒关闭（如果不配置，默认是3秒）
                    })
                }

            }
        })

        return false;
    })

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})